//
//  SinaRetweetView.swift
//  WeiBo01
//
//  Created by mac on 16/1/7.
//  Copyright © 2016年 Han. All rights reserved.
//

import UIKit
import SnapKit

class SinaRetweetView: UIView {

    //定义一个属性,来记录底部约束
    var picConstain: Constraint?
    
    var retweetViewModel: SinaStatusViewModel?{
        
        didSet{
            
            contentLabel.text = retweetViewModel?.model?.retweeted_status?.text
            
            //4.判断数据源
            
            //6.消除重用
            picConstain?.uninstall()
            
            if retweetViewModel?.model?.retweeted_status?.pic_urls?.count > 0{
                //5.根据数据源,实现配图的展示或隐藏
                pictureView.hidden = false
                
                snp_updateConstraints(closure: { (make) -> Void in
                    picConstain = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                //赋值
                pictureView.pictures = retweetViewModel?.model?.retweeted_status?.pic_urls
            }else{
                pictureView.hidden = true
                
                snp_updateConstraints(closure: { (make) -> Void in
                    picConstain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(contentLabel)
        //2.添加配图,设置约束
        addSubview(pictureView)
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.width.equalTo(self.snp_width)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.left.equalTo(self.snp_left).offset(SinaHomeMargin)
        }
        
        //3.修改转发微博的底部到contentLabel的底部
        self.snp_makeConstraints { (make) -> Void in
            picConstain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
        }
        
    }
    
    //MARK:--懒加载
    lazy var contentLabel: UILabel = {
       
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        
        return label
    }()
    
    //1.懒加载一个配图
    lazy var pictureView: SinaPictureView = SinaPictureView()

}
